package com.kede.api.stand.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.stand.service.ResourceService;
import com.kede.api.stand.service.SysAreaService;
import com.kede.domain.ResourceAreaMapDomain;
import com.kede.domain.ResourceFileDomain;
import com.kede.pojo.ResHttp;
import com.kede.service.ResourceAreaMapService;
import com.kede.service.ResourceFileService;
import com.kede.utils.CollectionUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 独立服务
 *
 * @author 11856
 * @date 2023/06/12
 */
@RestController
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @Resource
    private SysAreaService sysAreaService;

    @Resource
    private ResourceAreaMapService resourceAreaMapService;

    @Resource
    private ResourceFileService resourceFileService;


    /**
     * 得到分享img地址
     *
     * @param title 标题
     * @param con   反对
     * @return {@link ResHttp}
     */
    @GetMapping("getShareImg")
    public ResHttp getShareImg(String title,String con){
        String url =  resourceService.createShareQrcode( title, con);
        return ResHttp.okData(url);
    }

    /**
     * 默认全国地区编码
     */
    private static final Integer CHINAADCODE = 0;


    /**
     * 根据地区编码获取资源信息
     * 是区级的编码
     * @Author: Pr.li
     * @Date: 2023/6/3 11:05
     * @Param: [java.lang.Integer]
     * @Return: com.kede.pojo.ResHttp
     ***/
    @GetMapping("getResourceByAdCode")
    public ResHttp getResourceByAdCode(Integer adcode){
        LambdaQueryWrapper<ResourceAreaMapDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourceAreaMapDomain::getAreaId,adcode);
        List<Integer> collect = resourceAreaMapService.list(queryWrapper).stream().map(ResourceAreaMapDomain::getResourceId).collect(Collectors.toList());
        if (CollectionUtil.NotBlank(collect)){
            List<ResourceFileDomain> resourceFileDomains = resourceFileService.listByIds(collect);
            return ResHttp.okData(resourceFileDomains);
        }else {
            LambdaQueryWrapper<ResourceAreaMapDomain> allWrapper = new LambdaQueryWrapper<>();
            allWrapper.eq(ResourceAreaMapDomain::getAreaId,CHINAADCODE);
            List<Integer> all = resourceAreaMapService.list(allWrapper).stream().map(ResourceAreaMapDomain::getResourceId).collect(Collectors.toList());
            if (CollectionUtil.NotBlank(all)){
                List<ResourceFileDomain> resourceFileDomains = resourceFileService.listByIds(all);
                return ResHttp.okData(resourceFileDomains);
            }else {
                return ResHttp.okData(new ArrayList<>());
            }

        }

    }




}
